package com.fmsz.xmly.activitys.downloadactivity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fmsz.xmly.R;
import com.fmsz.xmly.fragments.downloadfgts.hotrecommendfgts.BaseHotRecommendFragment;
import com.fmsz.xmly.utils.SlideBackLayout;
import com.fmsz.xmly.utils.Urls;

import java.util.ArrayList;
import java.util.List;

public class HotRecommendActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private SearchView searchView;
    private RadioGroup radioGroup;
    private ImageView back;

    private FragmentManager manager;
    private FragmentTransaction transaction;
    private BaseHotRecommendFragment fragment;
    private List<Fragment> list;
    private String[] types = {Urls.TYPE_HOT, Urls.TYPE_RECENT, Urls.TYPE_CLASSIC};

    private SlideBackLayout mSlideBackLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_recommend);

        initRecommendView();
    }

    //初始化视图
    private void initRecommendView() {
        searchView = (SearchView) findViewById(R.id.searchView_recommend);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup_recommend);
        back = (ImageView) findViewById(R.id.btn_back_orange);

        back.setOnClickListener(this);

        list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            fragment = BaseHotRecommendFragment.newInstance(types[i]);
            list.add(fragment);
        }
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fragment_hot_rec, list.get(0));
        transaction.commit();

        radioGroup.setOnCheckedChangeListener(this);
        textColorChange(0);

        mSlideBackLayout = new SlideBackLayout(this);
        mSlideBackLayout.bind();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        transaction = manager.beginTransaction();
        switch (checkedId) {
            case R.id.hot_recommend:
                transaction.replace(R.id.fragment_hot_rec, list.get(0));
                textColorChange(0);
                break;
            case R.id.new_recommend:
                transaction.replace(R.id.fragment_hot_rec, list.get(1));
                textColorChange(1);
                break;
            case R.id.class_recommend:
                transaction.replace(R.id.fragment_hot_rec, list.get(2));
                textColorChange(2);
                break;
        }
        transaction.commit();
    }

    //文字颜色的变化
    public void textColorChange(int position) {
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            RadioButton button = (RadioButton) radioGroup.getChildAt(i);
            button.setTextColor(Color.RED);
            if (position == i) {
                button.setTextColor(Color.WHITE);
            }
        }
    }

    //返回箭头
    @Override
    public void onClick(View v) {
        finish();
    }

}
